package com.qianlou.qrcode.servlets;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.WriterException;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

@WebServlet("/github-generate-qrcode-with-color")
public class GithubGenerateQRCodeWithColor extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String text = req.getParameter("text");
        try {
            //彩色二维码
            Color defaultColor = Color.BLACK;
            String color = req.getParameter("color");
            String r = req.getParameter("r");
            String g = req.getParameter("g");
            String b = req.getParameter("b");
            switch (color) {
                case "red":
                    defaultColor = Color.RED;
                    break;
                case "green":
                    defaultColor = Color.GREEN;
                    break;
                case "blue":
                    defaultColor = Color.BLUE;
                    break;
                case "yellow":
                    defaultColor = Color.YELLOW;
                    break;
                case "pink":
                    defaultColor = Color.PINK;
                    break;
                case "orange":
                    defaultColor = Color.ORANGE;
                    break;
                case "gray":
                    defaultColor = Color.GRAY;
                    break;
                case "cyan":
                    defaultColor = Color.CYAN;
                    break;
                case "magenta":
                    defaultColor = Color.MAGENTA;
                    break;
                case "white":
                    defaultColor = Color.WHITE;
                    break;
                    case "purple":
                        defaultColor = new Color(137, 86, 137);
                        break;
                default:
                    break;
            }
            if (r != null && !r.isEmpty() && g != null && !g.isEmpty() && b != null && !b.isEmpty()) {

                defaultColor = new Color(Integer.parseInt(r), Integer.parseInt(g), Integer.parseInt(b));
            }
            BufferedImage image = QrCodeGenWrapper.of(text)
                    .setDrawPreColor(defaultColor)
                    .asBufferedImage();
            ImageIO.write(image, "png", resp.getOutputStream());
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
}
